﻿using System;
using System.ServiceModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnterpriseServices.DalEf;

namespace EnterpriseServices.Service
{

    public class ValidationServiceBase : IValidationManagerBase
    {
        #region IValidationManagerbase Members

        /// <summary>
        /// Returns true if the userName exists.
        /// </summary>
        /// <param name="userName">The username.</param>
        /// <returns></returns>
        public bool DoesUserExist(string userName)
        {
            try
            {
                using (ESDBEntities _esdb = new ESDBEntities())
                    return _esdb.ExtendedProcs.Users_DoesUserExists(userName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
                return false;
            }
        }

        /// <summary>
        /// Returns true if the userName exists.
        /// </summary>
        /// <param name="userName">The username.</param>
        /// <returns></returns>
        public bool DoesUserExistPass(string userName, string password)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Returns true if a userName belongs to the requested role.
        /// A userName belongs to a role if
        /// <list type="bullet">
        /// 		<item>the userName is directly attached to a role</item>
        /// 		<item>the groups the userName belongs to are attached to a role</item>
        /// 		<item>the groups' child groups are attached to a role</item>
        /// 		<item>the associated groups the userName belongs to are attached to a role</item>
        /// 	</list>
        /// Regarding Groups, a userName only belongs to a role if he is eiher a group owner or a group member.
        /// </summary>
        /// <param name="applicationName">Name of the application.</param>
        /// <param name="roleName">Name of the role.</param>
        /// <param name="userName">Name of the userName.</param>
        /// <returns></returns>
        public bool IsUserInRole(string applicationName, string roleName, string userName)
        {
            try
            {
                using (ESDBEntities _esdb = new ESDBEntities())
                    return _esdb.ExtendedProcs.Roles_IsUserInRole(applicationName, roleName, userName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
                return false;
            }
        }

        /// <summary>
        /// Returns true if a userName belongs to the requested role.
        /// A userName belongs to a role if
        /// <list type="bullet">
        /// 		<item>the userName is directly attached to a role</item>
        /// 		<item>the groups the userName belongs to are attached to a role</item>
        /// 		<item>the groups' child groups are attached to a role</item>
        /// 		<item>the associated groups the userName belongs to are attached to a role</item>
        /// 	</list>
        /// Regarding Groups, a userName only belongs to a role if he is eiher a group owner or a group member.
        /// </summary>
        /// <param name="applicationName">Name of the application.</param>
        /// <param name="roleName">Name of the role.</param>
        /// <param name="userName">Name of the userName.</param>
        /// <returns></returns>
        public bool IsUserInRolePass(string applicationName, string roleName, string userName, string password)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Determines whether [is group in role] [the specified application name].
        /// </summary>
        /// <param name="applicationName">Name of the application.</param>
        /// <param name="roleName">Name of the role.</param>
        /// <param name="groupName">Name of the group.</param>
        /// <returns>
        /// 	<c>true</c> if [is group in role] [the specified application name]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsGroupInRole(string applicationName, string roleName, string groupName)
        {
            //try
            //{
            //    using (ESDBEntities _esdb = new ESDBEntities())
            //        return _esdb.Gr (applicationName, roleName, userName);
            //}
            //catch (Exception ex)
            //{
            //    System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
            //    return false;
            //}
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the userNames by application.
        /// </summary>
        /// <param name="applicationName">Name of the application.</param>
        /// <returns></returns>
        public List<User> GetUsersByApplicationName(string applicationName)
        {
            try
            {
                using (ESDBEntities _esdb = new ESDBEntities())
                {
                    List<User> UserList = null;

                    UserList = _esdb.User_GetUsersByApplicationName(applicationName).ToList();

                    return UserList;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("EnterpriseServices", ex.Message, System.Diagnostics.EventLogEntryType.Error);
                return null;
            }
        }

        #endregion
    }
}
